<?php
/**
 * @version		$Id: hosting_accs.php 8 2011-12-23 15:25:34Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelHosting_accs extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'WhmaccId', 'a.WhmaccId',
				'WhmaccServer', 'a.WhmaccServer', 'server',
				'WhmaccUser', 'a.WhmaccUser',
				'WhmaccEmail', 'a.WhmaccEmail',
				'WhmaccDomain', 'a.WhmaccDomain',
				'WhmaccIp', 'a.WhmaccIp',
				'WhmaccPlan', 'a.WhmaccPlan',
				'WhmaccStatus', 'a.WhmaccStatus', 'status'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}
		
		$serverId = $app->getUserStateFromRequest($this->context.'.filter.server_id', 'filter_server_id');
		$this->setState('filter.server_id', $serverId);
		
		$planId = $app->getUserStateFromRequest($this->context.'.filter.plan_id', 'filter_plan_id');
		$this->setState('filter.plan_id', $planId);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		parent::populateState('a.WhmaccId', 'desc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->select($this->getState('list.select', 'a.*,
		o.OrderId AS order_no, o.OrderProduct AS order_prod, s.ServerName AS server
		FROM #__almh_whmaccounts AS a
		LEFT JOIN #__almh_orders AS o ON o.OrderAccount = a.WhmaccId
		LEFT JOIN #__almh_servers AS s ON s.ServerId = a.WhmaccServer
		'));		
		
		$serverId = $this->getState('filter.server_id');
		if (is_numeric($serverId)) {
			$query->where('a.WhmaccServer = '.$serverId);
		}
		
		$planId = $this->getState('filter.plan_id');
		if (is_numeric($planId)) {
			$query->where('a.WhmaccPlan = '.$planId);
		}
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.WhmaccStatus = '.$statusId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'WhmaccDomain:') === 0) {
				$query->where('a.WhmaccDomain = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.WhmaccDomain LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.WhmaccId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
	
	public function getServer() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('s.ServerId AS value, s.ServerName AS text');
		$query->from('#__almh_servers AS s');
		$query->join('INNER', '#__almh_whmaccounts AS a ON a.WhmaccServer = s.ServerId');
		$query->group('s.ServerId');
		$query->order('s.ServerName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}	
}

